<template>
  <view class="content">
    <scroll-view scroll-y class="main" @scrolltolower="onMore">
      <Navbar></Navbar>
      <Carousel :banners="banners" height="200px"></Carousel>
      <Category :category="category"></Category>
      <!--  热门推荐 -->
      <view class="panel recommend">
        <view class="item" v-for="item in hots" :key="item.id">
          <view class="title">
            {{ item.title }}<text>{{ item.alt }}</text>
          </view>
          <navigator
            hover-class="none"
            :url="`/pages/recommend/index?type=${item.type}`"
            class="cards"
          >
            <image
              mode="aspectFit"
              v-for="img in item.pictures"
              :key="img"
              :src="img"
            ></image>
          </navigator>
        </view>
      </view>

      <!-- 后面这里补上其他的渲染部分如新鲜好物, 如热门品牌 -->

      <!-- 猜你喜欢 -->
      <Guess :likes="likes" />
    </scroll-view>
  </view>
</template>

<script>
import Navbar from "./components/Navbar.vue";
import {
  getBannersAPI,
  getCategoryAPI,
  getHotsAPI,
  getLikesAPI,
} from "@/api/home";
export default {
  components: {
    Navbar,
  },
  data() {
    return {
      banners: [],
      category: [],
      hots: [],
      likes: [],
      page: 1,
      totalPage: null,
      loading: false,
    };
  },
  onLoad() {
    this.getBanners();
    this.getCategory();
    this.getHots();
    this.getLikes();
  },
  methods: {
    onMore() {
      console.log("触底了");
      if (this.loading) return;
      if (this.page > this.totalPage) {
        uni.showToast({
          title: "没有更多了",
          icon: "none",
        });
        return;
      }

      this.getLikes();
    },
    async getBanners() {
      const { result } = await getBannersAPI();
      console.log(result, "我是banners");
      this.banners = result;
    },
    async getCategory() {
      const { result } = await getCategoryAPI();
      console.log(result, "我是category");
      this.category = result;
    },
    async getHots() {
      const { result } = await getHotsAPI();
      console.log(result, "我是hots");
      this.hots = result;
    },
    async getLikes() {
      this.loading = true;
      const { result } = await getLikesAPI(this.page);
      const { items, pages } = result;
      this.totalPage = 3; //这里是为了实现测试，所以写死了翻到第三页就没有数据了
      console.log(items, "我是likes");
      this.likes = [...this.likes, ...items];
      this.page++;
      this.loading = false;
    },
  },
};
</script>

<style lang="scss" scoped>
.content {
  height: 100vh;
  display: flex;
  flex-direction: column;
}
.main {
  flex: 1;
  overflow: hidden;
  background-color: #f7f7f8;
}
/* 公共面板 */
.panel {
  margin: 20rpx 20rpx 0;
  border-radius: 10rpx;
  background-color: #fff;
  .title {
    display: flex;
    align-items: center;
    padding: 24rpx 24rpx 0;
    font-size: 32rpx;
    color: #262626;
    position: relative;
    text {
      font-size: 24rpx;
      color: #7f7f7f;
      margin-left: 18rpx;
    }
  }
  .more {
    position: absolute;
    right: 24rpx;
    font-size: 26rpx;
    color: #7f7f7f;
  }
  .cards {
    display: flex;
  }
  .name {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}
/* 推荐专区 */
.recommend {
  display: flex;
  flex-wrap: wrap;
  .item {
    display: flex;
    flex-direction: column;
    width: 50%;
    height: 254rpx;
    border-right: 1rpx solid #eee;
    border-top: 1rpx solid #eee;
    &:nth-child(2n) {
      border-right: 0 none;
    }
    &:nth-child(-n + 2) {
      border-top: 0 none;
    }
  }
  .cards {
    flex: 1;
    padding: 15rpx 20rpx;
    justify-content: space-between;
    image {
      width: 48%;
    }
  }
}

/* 新鲜好物 */
.fresh {
  min-height: 320rpx;
  .price {
    line-height: 1;
    text-align: left;
    font-size: 26rpx;
    color: #cf4444;
  }
  .small {
    font-size: 80%;
  }
}
.fresh .cards navigator,
.brands .cards navigator {
  width: 25%;
  padding: 15rpx 20rpx 20rpx;
  line-height: 1;
  text-align: center;
}
.fresh image,
.brands image {
  width: 126rpx;
  height: 126rpx;
}
.fresh .name,
.brands .name {
  margin: 20rpx 0 16rpx;
  font-size: 24rpx;
  color: #262626;
}
.brands {
  .price {
    font-size: 24rpx;
    color: #999;
  }
}
</style>
